package com.kaesun.ufit.tools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaesun.ufit.weight.R;

public class ToolbarView extends LinearLayout {

	private static final int IMAGE_HOUR[] = {
		R.drawable.img_time35_0, R.drawable.img_time35_1, R.drawable.img_time35_2, R.drawable.img_time35_3, R.drawable.img_time35_4,
		R.drawable.img_time35_5, R.drawable.img_time35_6, R.drawable.img_time35_7, R.drawable.img_time35_8, R.drawable.img_time35_9,
	};
	private static final int IMAGE_MINUTE[] = {
		R.drawable.img_time28_0, R.drawable.img_time28_1, R.drawable.img_time28_2, R.drawable.img_time28_3, R.drawable.img_time28_4,
		R.drawable.img_time28_5, R.drawable.img_time28_6, R.drawable.img_time28_7, R.drawable.img_time28_8, R.drawable.img_time28_9,
	};

	private TextView mTextViewTitle = null;
	private TextView mTextViewClock = null;
	private ImageView mHour10 = null;
	private ImageView mHour1 = null;
	private ImageView mColon = null;
	private ImageView mMinute10 = null;
	private ImageView mMinute1 = null;
	private ImageView mAmPm = null;

	private boolean mIsImage = false;
	private int mHour = 0;
	private int mMinute = 0;
	private boolean mBlink = false;

	public ToolbarView(Context context) {
		super(context);
		init(context);
	}

	public ToolbarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}


	public void setTitle(String title) {
		mTextViewTitle.setText(title);
	}
	public void setTitleSize(float size) {
		mTextViewTitle.setTextSize(size);

	}

	public void setTime(int hour, int minute) {
		if (mIsImage) {
			if (mHour != hour) {
				if (12 <= hour) {
					hour %= 12;
					mAmPm.setImageResource(R.drawable.img_time_pm);
				} else {
					if (0 == hour) {
						hour = 12;
					}
					mAmPm.setImageResource(R.drawable.img_time_am);
				}
				mHour10.setImageResource(IMAGE_HOUR[hour / 10]);
				mHour1.setImageResource(IMAGE_HOUR[hour % 10]);
				mHour = hour;
			}

			if (mMinute != minute) {
				mMinute10.setImageResource(IMAGE_MINUTE[minute / 10]);
				mMinute1.setImageResource(IMAGE_MINUTE[minute % 10]);
				mMinute = minute;
			}

			mColon.setVisibility(mBlink ? View.VISIBLE : View.INVISIBLE);
		} else {
			boolean isPm = (12 <= hour) ? true : false;
			if (12 < hour) {
				hour %= 12;
			} else if (0 == hour) {
				hour = 12;
			}
			if (mBlink) {
				mTextViewClock.setText(String.format("%02d:%02d %s", hour, minute, isPm ? "PM" : "AM"));
			} else {
				mTextViewClock.setText(String.format("%02d %02d %s", hour, minute, isPm ? "PM" : "AM"));
			}
		}

		mBlink = !mBlink;
	}

	public void chage2Button() {
		findViewById(R.id.button_warning).setVisibility(View.GONE);
		findViewById(R.id.button_guide).setVisibility(View.GONE);

		findViewById(R.id.button_home).setVisibility(View.VISIBLE);
	}

	private void init(Context context) {
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.view_toolbar, this);

		mTextViewTitle = (TextView)findViewById(R.id.text_toolbar_title);
		mTextViewClock = (TextView)findViewById(R.id.text_toolbar_clock);
		mHour10 = (ImageView)findViewById(R.id.image_clock_hour10);
		mHour1 = (ImageView)findViewById(R.id.image_clock_hour1);
		mColon = (ImageView)findViewById(R.id.image_clock_colon);
		mMinute10 = (ImageView)findViewById(R.id.image_clock_minute10);
		mMinute1 = (ImageView)findViewById(R.id.image_clock_minute1);
		mAmPm = (ImageView)findViewById(R.id.image_clock_ampm);

		if (mIsImage) {
			mTextViewClock.setVisibility(View.GONE);
		} else {
			mTextViewClock.setText("");
			findViewById(R.id.layout_toolbar_clock).setVisibility(View.GONE);
		}
	}
}
